// echo partial file, should not compile along

using System;
using System.Reflection;
using System.Reflection.Emit;
using System.Collections.Generic;
using HarmonyLib;
using Utils;
namespace GongfaEffect;

using GameData.Domains;

public partial class GongfaEffect {
//! 我终于开始对战斗功法下毒手了
    public static class TaiJiQuan {
        public static short Defkey_TaiJiQuan=344; // 目前这个方法并没有被暴露在defkey之外，因此只能如此使用。
        public static void Patch(RobustHarmonyInstance HarmonyInstance, string ModIdStr){
            HarmonyInstance.PatchAll(System.Reflection.MethodBase.GetCurrentMethod().ReflectedType);
        }
        public class MetAncestor {
            // public GameData.Domains.SpecialEffect.CombatSkill.Wudangpai.FistAndPalm.TaiJiQuan skill;
            public static FieldInfo _objects=typeof(GameData.Domains.Character.CharacterDomain).GetField("_objects",(BindingFlags)(-1));
            public int CharacterId;
            public short SkillTemplateId;
            public bool IsDirect;
            public GameData.Domains.Map.Location loc;
            public MetAncestor(GameData.Domains.SpecialEffect.CombatSkill.Wudangpai.FistAndPalm.TaiJiQuan skill, bool IsDirect){
                this.CharacterId=skill.CharacterId;
                this.SkillTemplateId=skill.SkillKey.SkillTemplateId;
                this.IsDirect=IsDirect;
                this.loc=skill.CharObj.GetLocation();
            }
            public void Init() {
                GameData.DomainEvents.Events.RegisterHandler_CastSkillEnd(this.ocse);
            }
            public void ocse(GameData.Common.DataContext context, int charId, bool isAlly, short skillId, sbyte power, bool interrupted) {
                if(charId == this.CharacterId && skillId==this.SkillTemplateId){
                    var TaiwuId=DomainManager.Taiwu.GetTaiwuCharId();
                    var chars=((Dictionary<int, GameData.Domains.Character.Character>)_objects.GetValue(DomainManager.Character));
                    if(chars==null){throw new Exception("chars is null");}
                    foreach(var victim in chars.Values){
                        if(victim!=null && (IsDirect^(victim.GetLocation()==this.loc))){
                            if(victim.GetId()!=TaiwuId)victim.SetBaseMaxHealth(1,context);
                        }
                    }
                    GameData.DomainEvents.Events.UnRegisterHandler_CastSkillEnd(this.ocse);
                    // GameData.Utilities.AdaptableLog.Info("MetAncestor.ocse("+this.CharacterId+")-执行完毕");
                }
            }
        }
        [HarmonyPatch(typeof(GameData.Domains.SpecialEffect.CombatSkill.Wudangpai.FistAndPalm.TaiJiQuan),"OnEnable")]
        public static void Postfix(GameData.Domains.SpecialEffect.CombatSkill.Wudangpai.FistAndPalm.TaiJiQuan __instance, bool ___IsDirect) {
            new MetAncestor(__instance, ___IsDirect).Init();
        }
    }
}
